CREATE TABLE [dbo].[Story](
	[Id] [int] IDENTITY(1,1) NOT NULL,
	[Name] [nvarchar](max) NOT NULL,
 CONSTRAINT [PK_Story] PRIMARY KEY CLUSTERED 
(
	[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

GO


CREATE TABLE [dbo].[Fact](
	[Id] [int] IDENTITY(1,1) NOT NULL,
	[Text] [nvarchar](max) NULL,
	[Date] [datetime2](0) NULL,
	[Rate] [smallint] NULL,
 CONSTRAINT [PK_Fact] PRIMARY KEY CLUSTERED 
(
	[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

GO

CREATE TABLE [dbo].[FactRelation](
	[Id] [int] IDENTITY(1,1) NOT NULL,
	[Fact1Id] [int] NOT NULL,
	[Fact2Id] [int] NOT NULL,
 CONSTRAINT [PK_FactRelation] PRIMARY KEY CLUSTERED 
(
	[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

ALTER TABLE [dbo].[FactRelation]  WITH CHECK ADD  CONSTRAINT [FK_FactRelation_Fact] FOREIGN KEY([Fact2Id])
REFERENCES [dbo].[Fact] ([Id])
GO

ALTER TABLE [dbo].[FactRelation] CHECK CONSTRAINT [FK_FactRelation_Fact]
GO

ALTER TABLE [dbo].[FactRelation]  WITH CHECK ADD  CONSTRAINT [FK_FactRelation_Fact1] FOREIGN KEY([Fact1Id])
REFERENCES [dbo].[Fact] ([Id])
GO

ALTER TABLE [dbo].[FactRelation] CHECK CONSTRAINT [FK_FactRelation_Fact1]


CREATE TABLE [dbo].[StoryFact](
	[Id] [int] IDENTITY(1,1) NOT NULL,
	[StoryId] [int] NOT NULL,
	[FactId] [int] NOT NULL,
 CONSTRAINT [PK_StoryFact] PRIMARY KEY CLUSTERED 
(
	[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

ALTER TABLE [dbo].[StoryFact]  WITH CHECK ADD  CONSTRAINT [FK_StoryFact_Fact] FOREIGN KEY([FactId])
REFERENCES [dbo].[Fact] ([Id])
GO

ALTER TABLE [dbo].[StoryFact] CHECK CONSTRAINT [FK_StoryFact_Fact]
GO

ALTER TABLE [dbo].[StoryFact]  WITH CHECK ADD  CONSTRAINT [FK_StoryFact_Story] FOREIGN KEY([StoryId])
REFERENCES [dbo].[Story] ([Id])
GO

ALTER TABLE [dbo].[StoryFact] CHECK CONSTRAINT [FK_StoryFact_Story]
GO


